<template>
  <div>
    <van-nav-bar fixed>
   <template #left>
    <img :src="imgLogo" alt="" class="logo">
   </template>
   <template #right>
<van-icon name="search" size="0.48rem" color="#fff" @click="$router.push('/search')"/>
   </template>
    </van-nav-bar>
    <van-tabs
    v-model="channelId"
      animated
      sticky
      offset-top="1.226667rem">
      <!-- :name='item.id' tabs组件v-model默认绑定的值是tab中的name字段
            后端调用接口时需要当前频道id
       -->
  <van-tab v-for="item in channelList" :title="item.name" :key='item.id' :name='item.id'>
    <!-- 内容 {{ item.name }} -->
    <ArticleList :cid='channelId'></ArticleList>
  </van-tab>
</van-tabs>
<van-icon name="plus" size="0.37333334rem" class="moreChannels" @click="show = true"/>
 <!-- 弹出层组件 -->
    <van-popup v-model="show" class="edit_wrap" @closed='onPopupClosed'>
      <!-- 弹出层的主体区域 -->
      <channel-edit ref='ChannelEditRef' @removeChannel='removeChannelFn' @changeChannel='changeChannelFn' @addChannel='addChannelFn' :userChannelList='channelList' @close='show=false'></channel-edit>
    </van-popup>
  </div>
</template>

<script>
import ChannelEdit from './ChannelEdit.vue'
import ArticleList from './ArticleList.vue'
import { userChannelApi, updateChannelAPI } from '@/api'
import logoPng from '../../assets/toutiao_logo.png'
export default {
  components: {
    ArticleList,
    ChannelEdit
  },
  data () {
    return {
      imgLogo: logoPng,
      // active: 0,
      channelList: [],
      channelId: 0,
      show: false
    }
  },
  async created () {
    // 文章频道列表
    const res = await userChannelApi()
    console.log(res)
    const { channels } = res.data.data
    this.channelList = channels
  },
  methods: {
    // 添加频道
    async addChannelFn (val) {
      this.channelList.push(val)
      this.updateChannel()
    },
    removeChannelFn (val) {
      const index = this.channelList.findIndex(item => item.id === val.id)
      console.log(index)
      this.channelList.splice(index, 1)
    },
    async updateChannel () {
      // 目标： 将更新后的数据传入后端接口
      // 根据接口所需要的数据格式传递参数  (后端规定的)
      // 1.需要去掉添加频道
      // 2.需要将seq添加到每一个对象中 seq是当前的顺序 index + 1
      // 3.传入到后端数据中不能有name 需要delete name
      // 4.调用接口的时候需要在外层包一个channels
      const list = this.channelList.filter(item => {
        return item.id !== 0
      })
      const resArr = list.map((item, index) => {
        const newObj = { ...item }
        newObj.seq = index + 1
        delete newObj.name
        return newObj
      })
      console.log(resArr)
      await updateChannelAPI({
        channels: resArr
      })
    },
    onPopupClosed () {
      // console.log(this.$refs.ChannelEditRef)
      // this.$refs 用于获取当前组件的实例
      // 这个实例包含(dom, 方法, 变量, 属性)
      // 也是父子传值的方法之一
      this.$refs.ChannelEditRef.isEdit = false
    },
    changeChannelFn (val) {
      this.channelId = val.id
      // 这个地方的vant组件自动帮我们调用了接口
      // 不需要自己重现调接口
    }
  }
}
</script>
<style scoped lang="less">
.logo {
    width: 100px;
    height: 30px;
}
/* tab内容距离tab导航的距离 */
/deep/ .van-tabs__content{
    padding-top: 44px;
}
/deep/.van-tab__pane{
  text-align: center;
}
// 设置 tabs 容器的样式
/deep/ .van-tabs__wrap {
  padding-right: 30px;
  background-color: #fff;
}

// 设置小图标的样式
.moreChannels {
  position: fixed;
  top: 62px;
  right: 8px;
  z-index: 999;
}
.edit_wrap{
  width: 100%;
  height: 100%;
}
</style>
